package com.tuanzi.loan.web.bean.family;

import com.base.common.log.LogService;
import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.common.CommProductChannel;
import com.tuanzi.loan.business.entity.family.LoanChannelNdInfo;
import com.tuanzi.loan.business.service.family.LargeAmountChannelManageService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.family.LargeAmountChannelManageSearchRequest;
import com.tuanzi.loan.vo.response.family.LargeAmountChannelManageSearchResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.family.LargeAmountChannelManageLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;
import java.util.Objects;

import static com.tuanzi.loan.business.constants.DictionaryCode.PRODUCT_MONEY;
/**
* @Description:    大额渠道配置后台
* @Author:         xionghao
* @CreateDate:     2018-09-27
*/
@Debug
@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LargeAmountChannelManageBean
        extends JSFListingBean<LargeAmountChannelManageSearchRequest,LargeAmountChannelManageSearchResponse>{
    @Autowired
    LargeAmountChannelManageService largeAmountChannelManageService;
    List<CommProductChannel> channelList = Lists.newArrayList();

    @PostConstruct
    private void init() {
        addBatchChannel();
        initLazyDataModel();
    }
    private void initLazyDataModel() {
        $ = new LargeAmountChannelManageSearchRequest();
        lazyDataModel = new LargeAmountChannelManageLazyDataModel($);
    }


    public void onEditTarget(LargeAmountChannelManageSearchResponse response){
        $.setAmountInterval(response.getAmountInterval());
        $.setAppChannel(response.getAppChannel());
        $.setChannelName(response.getChannelName());
        $.setId(Long.valueOf(String.valueOf(response.getId())));
        $.setProductName(response.getProductName());
        $.setPrdId(response.getPrdId());
        getChannel();
    }

    public void onCreateTarget(){
        $.setPrdId("8000");
        $.setAppChannel(null);
        $.setAmountInterval(null);
        $.setChannelName(null);
        getChannel();

    }

    public void onDelete(LargeAmountChannelManageSearchResponse response){
        largeAmountChannelManageService.deleteByid(response);
    }

    public void onEdit(){
        $.setProductName(translateProduct(Integer.parseInt($.getPrdId())));
        String max = dictionaryService.findByCodeAndName(PRODUCT_MONEY,$.getAmountInterval()).getValue2();
        if (StringUtils.isNotBlank(max)){
            $.setMaxAmount(Integer.parseInt(max));
        }
        String mix = dictionaryService.findByCodeAndName(PRODUCT_MONEY,$.getAmountInterval()).getValue1();
        if (StringUtils.isNotBlank(mix)) {
            $.setMinAmount(Integer.valueOf(mix));
        }
        try {
            if (largeAmountChannelManageService.updateLargeAmountChannelManage($)>0){
                addSuccessfullyMessage();
            }
        } catch (Exception e) {
           addErrorMessage("金额区间和外推渠道不可同时重复");
           LogService.error("渠道配置修改失败",e);
        }
        initLazyDataModel();
    }

    public void onCreate(){
        $.setProductName(translateProduct(Integer.parseInt($.getPrdId())));
        String max = dictionaryService.findByCodeAndName(PRODUCT_MONEY,$.getAmountInterval()).getValue2();
        if (StringUtils.isNotBlank(max)){
            $.setMaxAmount(Integer.parseInt(max));
        }
        String mix = dictionaryService.findByCodeAndName(PRODUCT_MONEY,$.getAmountInterval()).getValue1();
        if (StringUtils.isNotBlank(mix)) {
            $.setMinAmount(Integer.valueOf(mix));
        }

        try {
            if (Objects.nonNull(largeAmountChannelManageService.saveLargeAmountChannelManage($))){
                $=new LargeAmountChannelManageSearchRequest();
                addSuccessfullyMessage();
            }
        } catch (Exception e) {
            addErrorMessage("创建渠道失败");
            LogService.error("渠道配置创建失败",e);
        }
        initLazyDataModel();

    }
    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(PRODUCT_MONEY);
    }

    public List<String> selectDistinctCFrom(){
        return largeAmountChannelManageService.selectDistinctCFrom();
    }

    public void getChannel(){
        if (!channelList.isEmpty()){
            channelList.clear();
        }
        channelList= largeAmountChannelManageService.getChannelInfo(Integer.parseInt($.getPrdId()));
    }


    public void addChannelND(){
        LoanChannelNdInfo info = new LoanChannelNdInfo();
        info.setChannel_nd($.getChannel_nd());
        try {
            if (largeAmountChannelManageService.addChannelND(info)>0){
                addSuccessfullyMessage();
            }
        } catch (Exception e) {
            addErrorMessage("增加渠道错误");
            LogService.error("增加新渠道异常",e);
        }
        $=new LargeAmountChannelManageSearchRequest();
    }

    public void addBatchChannel(){
        try {
            largeAmountChannelManageService.addBatchChannelNd();
        } catch (Exception e) {
            LogService.error("批量增加渠道失败",e);
        }
    }

}
